package com.xiaobai.excel.handler;

import com.alibaba.excel.metadata.data.DataFormatData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.*;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 设置表头和填充内容的样式
 */
public class CellStyleStrategy extends HorizontalCellStyleStrategy {



    private final WriteCellStyle headWriteCellStyle;
    private final WriteCellStyle contentWriteCellStyle;

    private final Short headBackColor;

    private final Short contentBackColor;

    private final List<Short> headFieldBackColor;

    private final List<Short> contentFieldBackColor;

    private final Short headFontColor;

    private final Short contentFontColor;

    private final List<Short> headFieldFontColor;

    private final List<Short> contentFieldFontColor;

    private final Short headFontSize;

    private final Short contentFontSize;

    private final List<Short> headFieldFontSize;

    private final List<Short> contentFieldFontSize;

    private final Boolean headFontBold;

    private final Boolean contentFontBold;

    private final List<Boolean> headFieldFontBold;

    private final List<Boolean> contentFieldFontBold;

    /**
     * 构造style
     * @param headWriteCellStyle
     * @param contentWriteCellStyle
     * @param headBackColor 通用标题背景色
     * @param contentBackColor 通用内容背景色
     * @param headFieldBackColor 字段标题背景色
     * @param contentFieldBackColor 字段内容背景色
     * @param headFontColor 通用标题字体颜色
     * @param contentFontColor 通用内容字体颜色
     * @param headFieldFontColor 字段标题字体颜色
     * @param contentFieldFontColor 字段内容字体颜色
     * @param headFontSize 通用标题字体大小
     * @param contentFontSize 通用内容字体大小
     * @param headFieldFontSize 字段标题字体大小
     * @param contentFieldFontSize 字段内容字体大小
     * @param headFontBold  通用标题字体加粗
     * @param contentFontBold 通用内容字体加粗
     * @param headFieldFontBold 字段标题字体加粗
     * @param contentFieldFontBold 字段内容字体加粗
     */
    public CellStyleStrategy(WriteCellStyle headWriteCellStyle
            , WriteCellStyle contentWriteCellStyle
    , Short headBackColor, Short contentBackColor, List<Short> headFieldBackColor, List<Short> contentFieldBackColor, Short headFontColor, Short contentFontColor, List<Short> headFieldFontColor, List<Short> contentFieldFontColor, Short headFontSize, Short contentFontSize, List<Short> headFieldFontSize, List<Short> contentFieldFontSize, Boolean headFontBold, Boolean contentFontBold, List<Boolean> headFieldFontBold, List<Boolean> contentFieldFontBold) {
        this.headWriteCellStyle = headWriteCellStyle;
        this.contentWriteCellStyle = contentWriteCellStyle;
        this.headBackColor = headBackColor;
        this.contentBackColor = contentBackColor;
        this.headFieldBackColor = headFieldBackColor;
        this.contentFieldBackColor = contentFieldBackColor;
        this.headFontColor = headFontColor;
        this.contentFontColor = contentFontColor;
        this.headFieldFontColor = headFieldFontColor;
        this.contentFieldFontColor = contentFieldFontColor;
        this.headFontSize = headFontSize;
        this.contentFontSize = contentFontSize;
        this.headFieldFontSize = headFieldFontSize;
        this.contentFieldFontSize = contentFieldFontSize;
        this.headFontBold = headFontBold;
        this.contentFontBold = contentFontBold;
        this.headFieldFontBold = headFieldFontBold;
        this.contentFieldFontBold = contentFieldFontBold;
    }

    //设置头样式
    @Override
    protected void setHeadCellStyle(CellWriteHandlerContext context) {
        // 获取字体实例
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontName("宋体");
        short fontSize;
        if (CollectionUtils.isEmpty(headFieldFontSize)) {
            fontSize = headFontSize == null ? 14:  headFontSize;
        } else {
            final Short aShort = headFieldFontSize.get(context.getColumnIndex());
            fontSize = aShort == null ? (headFontSize == null ? 14:  headFontSize) : aShort;
        }
        headWriteFont.setFontHeightInPoints(fontSize);
        boolean bold;
        if (CollectionUtils.isEmpty(headFieldFontBold)) {
            bold = headFontBold != null && headFontBold;
        } else {
            final Boolean aShort = headFieldFontBold.get(context.getColumnIndex());
            bold = aShort == null ? (headFontBold != null && headFontBold) : aShort;
        }
        headWriteFont.setBold(bold);
        short fontColor;
        if (CollectionUtils.isEmpty(headFieldFontColor)) {
            fontColor = headFontColor == null ? IndexedColors.BLACK.getIndex() : headFontColor;
        } else {
            final Short aShort = headFieldFontColor.get(context.getColumnIndex());
            fontColor = aShort == null ? (headFontColor == null ? IndexedColors.BLACK.getIndex() : headFontColor) : aShort;
        }
        headWriteFont.setColor(fontColor);
        short backColor;
        if (CollectionUtils.isEmpty(headFieldBackColor)) {
            backColor = headBackColor == null ? IndexedColors.WHITE.getIndex() : headBackColor;
        } else {
            final Short aShort = headFieldBackColor.get(context.getColumnIndex());
            backColor = aShort == null ? (headBackColor == null ? IndexedColors.WHITE.getIndex() : headBackColor) : aShort;
        }
        headWriteCellStyle.setFillForegroundColor(backColor);
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        headWriteCellStyle.setWriteFont(headWriteFont);
        DataFormatData dataFormatData = new DataFormatData();
        dataFormatData.setIndex((short)49);
        headWriteCellStyle.setDataFormatData(dataFormatData);
        if (stopProcessing(context)) {
            return;
        }
        WriteCellData<?> cellData = context.getFirstCellData();
        WriteCellStyle.merge(headWriteCellStyle, cellData.getOrCreateStyle());
    }

    //设置填充数据样式
    @Override
    protected void setContentCellStyle(CellWriteHandlerContext context) {
        WriteFont contentWriteFont = new WriteFont();
        contentWriteFont.setFontName("宋体");
        short fontSize;
        if (CollectionUtils.isEmpty(contentFieldFontSize)) {
            fontSize = contentFontSize == null ? 11:  contentFontSize;
        } else {
            final Short aShort = contentFieldFontSize.get(context.getColumnIndex());
            fontSize = aShort == null ? (contentFontSize == null ? 11:  contentFontSize) : aShort;
        }
        contentWriteFont.setFontHeightInPoints(fontSize);
        boolean bold;
        if (CollectionUtils.isEmpty(contentFieldFontBold)) {
            bold = contentFontBold != null && contentFontBold;
        } else {
            final Boolean aShort = contentFieldFontBold.get(context.getColumnIndex());
            bold = aShort == null ? (contentFontBold != null && contentFontBold) : aShort;
        }
        contentWriteFont.setBold(bold);
        short fontColor;
        if (CollectionUtils.isEmpty(contentFieldFontColor)) {
            fontColor = contentFontColor == null ? IndexedColors.BLACK.getIndex() : contentFontColor;
        } else {
            final Short aShort = contentFieldFontColor.get(context.getColumnIndex());
            fontColor = aShort == null ? (contentFontColor == null ? IndexedColors.BLACK.getIndex() : contentFontColor) : aShort;
        }
        contentWriteFont.setColor(fontColor);

        short backColor;
        if (CollectionUtils.isEmpty(contentFieldBackColor)) {
            backColor = contentBackColor == null ? IndexedColors.WHITE.getIndex() : contentBackColor;
        } else {
            final Short aShort = contentFieldBackColor.get(context.getColumnIndex());
            backColor = aShort == null ? (contentBackColor == null ? IndexedColors.WHITE.getIndex() : contentBackColor) : aShort;
        }
        contentWriteCellStyle.setFillForegroundColor(backColor);
        contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        //设置数据填充后的实线边框
        contentWriteCellStyle.setWriteFont(contentWriteFont);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        DataFormatData dataFormatData = new DataFormatData();
        dataFormatData.setIndex((short)49);
        contentWriteCellStyle.setDataFormatData(dataFormatData);
        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        contentWriteCellStyle.setWrapped(true);
        WriteCellData<?> cellData = context.getFirstCellData();
        WriteCellStyle.merge(contentWriteCellStyle, cellData.getOrCreateStyle());
    }
}
